<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        
        img {
            position: absolute;
            top: 250px;
        }
    </style>
</head>

<body>
    <div></div>
    <br>
    <br>
    <img src="拖拽元素.png" alt="">
    <script>
        var div = document.querySelector('div');
        // 手指初始坐标
        var startX = 0;
        var startY = 0;
        // 盒子原来的坐标
        var x = 0;
        var y = 0;
        div.addEventListener('touchstart', function(e) {
            startX = e.targetTouches[0].pageX;
            startY = e.targetTouches[0].pageY;
            x = this.offsetLeft;
            y = this.offsetTop;
        });

        div.addEventListener('touchmove', function(e) {
            // 计算移动距离
            var moveX = e.targetTouches[0].pageX - startX;
            var moveY = e.targetTouches[0].pageY - startY;
            // 移动盒子
            this.style.left = x + moveX + 'px';
            this.style.top = y + moveY + 'px';
            // 拖拽时阻止屏幕滚动 (阻止默认事件)
            e.preventDefault();
        });
    </script>
</body>

</html>